package LeetCode.BitCompute;

public class Test3 {
    //数学方法：思路：数组相当于0到n+1，去掉了中间某个数，n为数组长度，
    // 所以只需求和0到n+1 减去 数组的和 即可。
    public static int missingNumber(int[] nums) {
        int sum1 = nums.length*(nums.length + 1)/2;
        int sum2 = 0;
        for (int num : nums) sum2 += num;
        return sum1 - sum2;
    }

    //位运算：
    public int missingNumber2(int[] nums) {
        int ret = 0;
        for (int i = 0; i < nums.length; i++) {
            ret = ret ^ i ^ nums[i];
        }
        return ret ^ nums.length;
    }

    public static void main(String[] args) {
        System.out.println("位运算：缺失数字");
        int[] nums={9,6,4,2,3,5,7,0,1 };
        System.out.println(missingNumber(nums));
    }
}
